package com.hgkx.util;

import com.hgkx.domain.ContactUs;
import com.hgkx.domain.Mail;
import com.hgkx.domain.MessageBoard;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件发送工具类
 */
@AllArgsConstructor
@NoArgsConstructor
public class MailUtil {

    @Autowired
    public JavaMailSenderImpl mailSender;

    public static Mail mail=new Mail();

    //只有一行字的文件
    public void sendingMail(Mail mail) {
        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject(mail.getSubject());   //主题
        mailMessage.setText(mail.getText());    //文本
        mailMessage.setTo(mail.getTo());     //收件人
        mailMessage.setFrom(mail.getFrom());   //服务器   发件人

        mailSender.send(mailMessage);   //发送
    }

    //留言邮件
    @Async
    public void sendingMBMail(MessageBoard board) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper  = new MimeMessageHelper(mimeMessage,true);
        //正文
        helper.setSubject("哈工科讯物流装备有限公司门户网站-留言信息");
        helper.setText("<h1>哈工科讯物流装备有限公司</h1>\n" +
                "<h2>留言板</h2>\n" +
                "<p>姓名："+board.getName()+"</p>\n" +
                "<p>电话："+board.getPhone()+"</p>\n" +
                "<p>留言内容："+board.getRemark()+"</p>",true);

        helper.setTo(mail.getTo());
        helper.setFrom(mail.getFrom());

        mailSender.send(mimeMessage);
    }

    //联系邮件
    @Async
    public void sendingCUMail(ContactUs us) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper  = new MimeMessageHelper(mimeMessage,true);
        //正文
        helper.setSubject("哈工科讯物流装备有限公司门户网站-联系我们信息");
        helper.setText("<h1>哈工科讯物流装备有限公司</h1>\n" +
                "<h2>联系信息</h2>\n" +
                "<p>姓名："+us.getName()+"</p>\n" +
                "<p>邮箱："+us.getEmail()+"</p>\n" +
                "<p>电话："+us.getPhone()+"</p>\n" +
                "<p>行业："+us.getIndustry()+"</p>\n" +
                "<p>留言内容："+us.getRemark()+"</p>",true);

        helper.setTo(mail.getTo());
        helper.setFrom(mail.getFrom());

        mailSender.send(mimeMessage);
    }
}
